﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using MiUi.ShuaMi;
using System.Runtime.InteropServices;

namespace IpTest
{
    class Program
    {
        static List<string> list = new List<string>();
        static string fileName = "ipadess.xx";
        static void Main(string[] args)
        {

            //绑定控制台关闭事件
            ConsoleCtrlDelegate newDelegate = new ConsoleCtrlDelegate(HandlerRoutine);
            bool bRet = SetConsoleCtrlHandler(newDelegate, true);
            
            string url = System.Configuration.ConfigurationManager.AppSettings["IpTestUrl"];
          
            if (string.IsNullOrEmpty(url))
            {
                url = "http://gd.10086.cn/simis/ADCDL100WAP/DL100Index.aspx"+Environment.NewLine;
            }
            Console.WriteLine("测试地址:" + url);
            VoteLog.AddResult(fileName, url);
            int count = 0;
            while (true)
            {
                try
                {
                    WebClient webClient = new WebClient();
                    string ip = GetRandIp();
                    webClient.Headers.Add("X-Forwarded-For", ip);
                    byte[] responseData = webClient.DownloadData(url);
                    string srcString = Encoding.UTF8.GetString(responseData);
                    list.Add(ip);
                    if (count % 100 == 0 && count!=0)
                    {
                        VoteLog.AddResult(fileName, list.ToString(Environment.NewLine) + Environment.NewLine);
                        list.Clear();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Thread.Sleep(2000);
                }
                Console.WriteLine("休息0.2秒.." + count);
                Thread.Sleep(200);
                count++;
            }
        }


        /// <summary>
        /// 处理消息的事件
        /// </summary>
        private static bool HandlerRoutine(int CtrlType)
        {
            switch (CtrlType)
            {
                case CTRL_CLOSE_EVENT:      //用户要关闭Console了
                    Console.WriteLine("System is saving Setting...");
                    try
                    {
                        VoteLog.AddResult(fileName, list.ToString(Environment.NewLine) + Environment.NewLine);
                        list.Clear();
                    }
                    catch (Exception)
                    {
                    }
                    //do  something...
                    break;
            }
            return false;
        }   

        //委托
        public delegate bool ConsoleCtrlDelegate(int dwCtrlType);  

        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
        //当用户关闭Console时，系统会发送次消息
        private const int CTRL_CLOSE_EVENT = 2;


        private static string GetRandIp2()
        {
            Random rd = new Random();
            return string.Format("{0}.{1}.{2}.{3}", rd.Next(1, 255), rd.Next(1, 255), rd.Next(1, 255), rd.Next(1, 255));
        }

        private static string GetRandIp()
        {
            Random rd = new Random();
            return string.Format("88.88.{0}.{1}", rd.Next(1, 255), rd.Next(1, 255));
        }

    }
}
